package cn.glutnn.subject.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * Author:   msh
 * Date:     2024/11/20 下午8:46
 * Description: druid 加密工具类
 */

public class DruidEncryptUtil {
    private static final String publicKey;
    private static final String privateKey;

    static {
        try {
            String[] keyPair = ConfigTools.genKeyPair(512);
            privateKey = keyPair[0];
            System.out.println("privateKey:" + privateKey);
            publicKey = keyPair[1];
            System.out.println("publicKey:" + publicKey);
        } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 加密
     *
     * @param password
     * @return
     * @throws Exception
     */
    public static String encrypt(String password) throws Exception {
        String encrypt = ConfigTools.encrypt(privateKey, password);
        System.out.println("encrypt:" + encrypt);
        return encrypt;
    }

    /**
     * 解密
     *
     * @param password
     * @return
     * @throws Exception
     */
    public static String decrypt(String password) throws Exception {
        String decrypt = ConfigTools.decrypt(publicKey, password);
        System.out.println("decrypt:" + decrypt);
        return decrypt;
    }

    public static void main(String[] args) throws Exception {
        String password = "123456";
        String encrypt = encrypt(password);
        String decrypt = decrypt(encrypt);
    }
}
